package com.tjshop.android;

import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.tjshop.android.util.WaitProgressDialog;

public abstract class BaseActivity extends SherlockActivity implements
		Handler.Callback {
	public Handler mHandler;
	protected RequestQueue mQueue;
	protected WaitProgressDialog mProgressDialog;

	@Override
	protected void onCreate(Bundle arg0) {
		mHandler = new Handler(this);
		mQueue = Volley.newRequestQueue(this);
		mProgressDialog = new WaitProgressDialog(this);
		super.onCreate(arg0);
	}

	public void updateImageButtonBar(String title, int resId,
			OnClickListener listener) {
		if (title == null) {
			ActionBar actionBar = getSupportActionBar();
			getSupportActionBar().setDisplayShowCustomEnabled(false);
			getSupportActionBar().setDisplayShowHomeEnabled(false);
			actionBar.setDisplayShowCustomEnabled(false);
			return;
		}
		getSupportActionBar().setDisplayShowCustomEnabled(true);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		View v = LayoutInflater.from(this).inflate(R.layout.common_title_bar,
				null);
		TextView tv_left = (TextView) v
				.findViewById(R.id.common_title_bar_text_left);
		TextView tv_right = (TextView) v
				.findViewById(R.id.common_title_bar_right_icon_left);
		ImageView back = (ImageView) v
				.findViewById(R.id.common_title_bar_left_icon);
		ImageView menu = (ImageView) v
				.findViewById(R.id.common_title_bar_right_icon);

		tv_left.setText(title);
		tv_right.setVisibility(View.INVISIBLE);

		tv_left.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
				overridePendingTransition(R.anim.slide_right_in,
						R.anim.slide_right_out);
			}
		});
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
				overridePendingTransition(R.anim.slide_right_in,
						R.anim.slide_right_out);
			}
		});

		menu.setImageResource(resId);
		if (listener != null) {
			menu.setOnClickListener(listener);
		}

		com.actionbarsherlock.app.ActionBar.LayoutParams params = new com.actionbarsherlock.app.ActionBar.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		params.leftMargin = -70;
		getSupportActionBar().setCustomView(v, params);

	}

	public void updateTextViewBar(String title, String menuTitle,
			OnClickListener listener) {
		if (title == null) {
			ActionBar actionBar = getSupportActionBar();
			getSupportActionBar().setDisplayShowCustomEnabled(false);
			getSupportActionBar().setDisplayShowHomeEnabled(false);
			actionBar.setDisplayShowCustomEnabled(false);
			return;
		}
		getSupportActionBar().setDisplayShowCustomEnabled(true);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		View v = LayoutInflater.from(this).inflate(R.layout.common_title_bar,
				null);
		TextView tv_left = (TextView) v
				.findViewById(R.id.common_title_bar_text_left);
		TextView tv_right = (TextView) v
				.findViewById(R.id.common_title_bar_right_icon_left);
		ImageView back = (ImageView) v
				.findViewById(R.id.common_title_bar_left_icon);
		ImageView menu = (ImageView) v
				.findViewById(R.id.common_title_bar_right_icon);

		tv_left.setText(title);
		tv_left.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
				overridePendingTransition(R.anim.slide_right_in,
						R.anim.slide_right_out);
			}
		});

		menu.setVisibility(View.INVISIBLE);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
				overridePendingTransition(R.anim.slide_right_in,
						R.anim.slide_right_out);
			}
		});

		tv_right.setText(menuTitle);
		if (listener != null) {
			tv_right.setOnClickListener(listener);
		}

		com.actionbarsherlock.app.ActionBar.LayoutParams params = new com.actionbarsherlock.app.ActionBar.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		params.leftMargin = -70;
		getSupportActionBar().setCustomView(v, params);

	}

	public void updateCommonReturnBar(String title) {
		if (title == null) {
			ActionBar actionBar = getSupportActionBar();
			getSupportActionBar().setDisplayShowCustomEnabled(false);
			getSupportActionBar().setDisplayShowHomeEnabled(false);
			actionBar.setDisplayShowCustomEnabled(false);
			return;
		}
		getSupportActionBar().setDisplayShowCustomEnabled(true);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		View v = LayoutInflater.from(this).inflate(R.layout.common_title_bar,
				null);
		TextView tv_left = (TextView) v
				.findViewById(R.id.common_title_bar_text_left);
		TextView tv_right = (TextView) v
				.findViewById(R.id.common_title_bar_right_icon_left);
		ImageView back = (ImageView) v
				.findViewById(R.id.common_title_bar_left_icon);
		ImageView menu = (ImageView) v
				.findViewById(R.id.common_title_bar_right_icon);

		tv_left.setText(title);
		tv_left.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
				overridePendingTransition(R.anim.slide_right_in,
						R.anim.slide_right_out);
			}
		});
		tv_right.setVisibility(View.INVISIBLE);
		menu.setVisibility(View.INVISIBLE);

		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
				overridePendingTransition(R.anim.slide_right_in,
						R.anim.slide_right_out);
			}
		});

		com.actionbarsherlock.app.ActionBar.LayoutParams params = new com.actionbarsherlock.app.ActionBar.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		params.leftMargin = -70;
		getSupportActionBar().setCustomView(v, params);

	}
	
	public void updateCommonReturnBar(String title,OnClickListener listener) {
		if (title == null) {
			ActionBar actionBar = getSupportActionBar();
			getSupportActionBar().setDisplayShowCustomEnabled(false);
			getSupportActionBar().setDisplayShowHomeEnabled(false);
			actionBar.setDisplayShowCustomEnabled(false);
			return;
		}
		getSupportActionBar().setDisplayShowCustomEnabled(true);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		View v = LayoutInflater.from(this).inflate(R.layout.common_title_bar,
				null);
		TextView tv_left = (TextView) v
				.findViewById(R.id.common_title_bar_text_left);
		TextView tv_right = (TextView) v
				.findViewById(R.id.common_title_bar_right_icon_left);
		ImageView back = (ImageView) v
				.findViewById(R.id.common_title_bar_left_icon);
		ImageView menu = (ImageView) v
				.findViewById(R.id.common_title_bar_right_icon);

		tv_left.setText(title);
		if(listener !=null){
			tv_left.setOnClickListener(listener);
		}else{
			tv_left.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					finish();
					overridePendingTransition(R.anim.slide_right_in,
							R.anim.slide_right_out);
				}
			});
		}
		
		tv_right.setVisibility(View.INVISIBLE);
		menu.setVisibility(View.INVISIBLE);

		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
				overridePendingTransition(R.anim.slide_right_in,
						R.anim.slide_right_out);
			}
		});

		com.actionbarsherlock.app.ActionBar.LayoutParams params = new com.actionbarsherlock.app.ActionBar.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		params.leftMargin = -70;
		getSupportActionBar().setCustomView(v, params);

	}

	@Override
	public void startActivity(Intent intent) {
		super.startActivity(intent);
		overridePendingTransition(R.anim.slide_left_in, R.anim.slide_left_out);
	}

	@Override
	public void onBackPressed() {
		finish();
		overridePendingTransition(R.anim.slide_right_in, R.anim.slide_right_out);
		super.onBackPressed();
	}

	@Override
	public abstract boolean handleMessage(Message arg0);
}
